package com.czy.util;

import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chenzy
 * 集合工具类
 * @since 2020-05-11
 */
public class ListUtil {
    public static Boolean isEmpty(Object[] array) {
        return (array == null || array.length == 0);
    }

    public static <T> Boolean isEmpty(List<T> list) {
        return list == null || list.isEmpty();
    }

    public static <T> Boolean isEmpty(Set<T> list) {
        return list == null || list.isEmpty();
    }

    public static Boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    public static <T> Boolean isNotEmpty(List<T> list) {
        return !isEmpty(list);
    }

    public static <T> Boolean isNotEmpty(Object[] list) {
        return !isEmpty(list);
    }

    public static Boolean isEmptyOr(Object[]... array) {
        if (isEmpty(array)) {
            return true;
        }
        return Arrays.stream(array).anyMatch(ListUtil::isEmpty);
    }

    /*交集*/
    public static <T> List<T> intersect(List<T> list1, List<T> list2) {
        if (isEmpty(list1)||isEmpty(list2)) {
            return Collections.emptyList();
        }
        return list1.stream().filter(list2::contains).collect(Collectors.toList());
    }

    /*差集,list1-list2*/
    public static <T> List<T> except(List<T> list1, List<T> list2) {
        if (isEmpty(list1)) {
            return Collections.emptyList();
        }
        if (isEmpty(list2)) {
            return list1;
        }
        return list1.stream().filter(item -> !list2.contains(item)).collect(Collectors.toList());
    }

    /*去重并集*/
    public static <T> List<T> union(List<T> list1, List<T> list2) {
        boolean empty1=isEmpty(list1),empty2=isEmpty(list2);
        if (empty1&&empty2) {
            return Collections.emptyList();
        }
        if (empty1) {
            return list2;
        }
        if (empty2) {
            return list1;
        }
        list1.addAll(list2);
        return list1.stream().distinct().collect(Collectors.toList());
    }

    public static <T> T[] list2Array(List<T> list) {
        if (isEmpty(list)) {
            return null;
        }
        //noinspection unchecked
        return (T[]) list.toArray();
    }

    public static <T> List<T> array2List(T[] array) {
        return List.of(array);
    }
    public static MyList<Byte> byteArray2List(byte[] bytes){
        var result=new MyList<Byte>(bytes.length);
        for (var b:bytes){
            result.add(b);
        }
        return result;
    }
    public static Byte[] byteArray2Array(byte[] bytes){
        var result=new Byte[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            result[i]=bytes[i];
        }
        return result;
    }
    public static byte[] array2byteArray(Byte[] bytes){
        var result=new byte[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            result[i]=bytes[i];
        }
        return result;
    }
    /*十六进制数转成字节数组*/
    public static List<Byte> hex2Bytes(String hex) {
        return Stream.iterate(0, i -> i + 2).limit(hex.length() / 2)
                .map(i -> (byte) ((Byte.valueOf(hex.substring(i, i + 1), 16) << 4) ^ Integer.valueOf(hex.substring(i + 1, i + 2), 16)))
                .collect(Collectors.toList());

    }
    /*创建泛型数组*/
    @SuppressWarnings("unchecked")
    public static <T> T[] newArray(Class<T> tClass, int length) {
        return (T[]) Array.newInstance(tClass, length);
    }

    public static void main(String[] args) {
        var data=new byte[]{-86, 4, 2, 4, 0, 0, -72, 112, 85};
        var reuslt=byteArray2List(data);
        System.out.println(reuslt);
        var a= reuslt.toArray();
        System.out.println(Arrays.toString(a));
    }
}
